#include"pc.hpp"
#include<functional>
using func_t=function<void()>;
void print(){
    cout<<"打印中..."<<endl;
    sleep(1);
}
void download(){
    cout<<"下载中..."<<endl;
    sleep(1);
}
void getlog(){
    cout<<"获得日志中..."<<endl;
    sleep(1);
}
void browse(){
    cout<<"浏览中..."<<endl;
    sleep(1);
}
vector<func_t>task;
int cnt=0;

class thread_set{
    public:
     thread_set(string n, singqueue<func_t> &s):_name(n),_sq(s){}
     ~thread_set(){}
     string _name;
     singqueue<func_t> &_sq;
};

void * consumer(void *arg){
    thread_set* ts=static_cast< thread_set*>(arg);
   while(1){
    //生产者快：
    sleep(2);
   func_t tk= ts->_sq.pop();
   cout<<ts->_name<<" : ";
    tk();
     
   }
}  

void * producter(void *arg){
    thread_set* ts=static_cast< thread_set*>(arg);
     while(1){
        //消费者快
       // sleep(2);
        //cout<<"生产一个任务..."<<endl;
       ts->_sq.equeue(task[cnt%task.size()]);
        //生产者快：
        cout<<ts->_name<<" 生产一个任务..."<<endl;

        cnt++;
        
     }
}

int main(){
     task.push_back(print);
     task.push_back(download);
     task.push_back(getlog);
     task.push_back(browse);
     singqueue<func_t>sq;
     thread_set *ts1=new thread_set ("t1",sq);
     thread_set *ts2=new thread_set ("t2",sq);
     thread_set *ts3=new thread_set ("t3",sq);
     thread_set *ts4=new thread_set ("t4",sq);
     thread_set *ts5=new thread_set ("t5",sq);

 // 两个消费者三个生产者
    pthread_t c[2],p[3];
    pthread_create(c,nullptr,consumer,ts1);
    pthread_create(c+1,nullptr,consumer,ts2);
    pthread_create(p,nullptr,producter,ts3);
    pthread_create(p+1,nullptr,producter,ts4);
    pthread_create(p+2,nullptr,producter,ts5);
    

    pthread_join(*c,nullptr);
    pthread_join(*(c+1),nullptr);

    pthread_join(*p,nullptr);
    pthread_join(*(p+1),nullptr);
    pthread_join(*(p+2),nullptr);
 





    
}